#   /**
#   * Copyright (c) 2022 Beijing Jiaotong University
#   * PhotLab is licensed under [Open Source License].
#   * You can use this software according to the terms and conditions of the [Open Source License].
#   * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#   *
#   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#   * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#   * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#   *
#   * See the [Open Source License] for more details.
#   */
#   /**
#   * Author: Zheng Wang
#   * Created: Sep. 28, 2022
#   * Supported by: National Key Research and Development Program of China
#   */

import os
import sys
import yaml
import logging


def _get_section(section_name):
    """
        get configuration file section object by section name
    :param section_name:
    :return:
    """

    # computing_engine的绝对路径
    top_module_abs_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    config_data = read_engine_config_file(os.path.join(top_module_abs_path, "configuration","engine_config.yaml"))
    return config_data[section_name]


def read_engine_config_file(file_path) -> dict:
    """
    parse engine configuration file
    :param file_path: a yaml type file contains engine configuration
    :return: dict type configuration object
    """
    with open(file_path, "r") as f:
        data = yaml.safe_load(f)
    return data


def read_configuration_item(config_dict: dict, configuration_item="logging_config"):
    """
    read configuration item
    :param config_dict: config dict has generated by read_engine_config_file(...) function
    :param configuration_item:
        1. logging_config
        2. waiting_strategy_config
    :return:
    """
    return config_dict[configuration_item]


def _gen_log_stream(stream_name):
    if "sys.stdout" == stream_name:
        return sys.stdout


def _gen_log_format(format_name):
    __default_log_format = "Process Id: %(process)d," \
                           " Process Name: %(processName)s," \
                           " Time: %(asctime)s," \
                           " Log Level: %(levelname)s," \
                           " File name: %(filename)s" \
                           " Line: %(lineno)d" \
                           " Message: %(message)s"

    if "default_format" == format_name:
        return __default_log_format


def _gen_filepath(filepath):
    if filepath == "default":
        py_phot_engine_abs_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
        return os.path.join(py_phot_engine_abs_path, "test_log.txt")
    else:
        return filepath


def load_logging_config():

    logging_config = _get_section("logging_config")
    filepath = _gen_filepath(logging_config["filepath"])
    res_conf_dict = {
        "output_direction": logging_config["output_direction"],
        "filepath": filepath,
        "stream": _gen_log_stream(logging_config["stream"]),
        "log_format": _gen_log_format(logging_config["log_format"]),
        "log_level": logging.getLevelName(logging_config["log_level"]),
    }
    return res_conf_dict


def load_waiting_strategy_config():
    """
        load computing engine waiting strategy configuration
        加载计算引擎等待策略配置
    :return:
    """
    # computing_engine的绝对路径

    waiting_strategy_config = _get_section("waiting_strategy_config")
    res_conf_dict = {
        "max_waiting_time": waiting_strategy_config["max_waiting_time"],
        "max_waiting_loop_number": waiting_strategy_config["max_waiting_loop_number"]
    }
    return res_conf_dict


if __name__ == '__main__':
    ENGINE_YAML_CONFIG_FILE_PATH = os.path.join(os.getcwd(), "engine_config.yaml")
    config_dict = read_engine_config_file(ENGINE_YAML_CONFIG_FILE_PATH)
    print(load_logging_config())
